var creator = creator || {};//模型创建对象
 

creator.freezeSelect = function(){
	freezeSelectNow[0] = [];
	freezeSelectNow[1] = [];
	freezeSelectNow[2] = [];
	for(var n=0;n < cubeSelectNow.length;n++){
		if(!cubeSelectNow[n][2][0]){
				freezeSelectNow[0].push(cubeSelectNow[n][2]);
				freezeSelectNow[1].push(cubeSelectNow[n][3]);
				freezeSelectNow[2].push(n);
				}
		}
	}

creator.freezeMatrix = function(){
	freezeSelectMatrix[0] = [];
	freezeSelectMatrix[1] = [];
	freezeSelectMatrix[2] = [];
	freezeSelectMatrix[4] = [];
	freezeSelectMatrix[2][0] = touchPicked[4].x;
	freezeSelectMatrix[2][1] = touchPicked[4].y;
	freezeSelectMatrix[2][2] = touchPicked[4].z;
	freezeSelectMatrix[3] = false;
	for(var n=0;n < cubeSelectNow.length;n++){
		if(cubeSelectNow[n][2][0]){
			var martrix = [cubeSelectNow[n][2].localX,cubeSelectNow[n][2].localY,cubeSelectNow[n][2].localZ];
			freezeSelectMatrix[0].push(cubeSelectNow[n][2]);
			freezeSelectMatrix[1].push(martrix);
			freezeSelectMatrix[4].push(n);
			}
		}
	}
	
creator.freezeMatrixCubeAndMass = function(){
	freezeSelectMatrix[0] = [];
	freezeSelectMatrix[1] = [];
	freezeSelectMatrix[2] = [];
	freezeSelectMatrix[4] = [];
	freezeSelectMatrix[2][0] = touchPicked[4].x;
	freezeSelectMatrix[2][1] = touchPicked[4].y;
	freezeSelectMatrix[2][2] = touchPicked[4].z;
	freezeSelectMatrix[3] = false;
	for(var n=0;n < cubeSelectNow.length;n++){
			var martrix = [cubeSelectNow[n][2].localX,cubeSelectNow[n][2].localY,cubeSelectNow[n][2].localZ];
			freezeSelectMatrix[0].push(cubeSelectNow[n][2]);
			freezeSelectMatrix[1].push(martrix);
			freezeSelectMatrix[4].push(n);
		}
	}

	
creator.clearCoorArray = function(mass){
	for(var n = 0;n < mass.length;n++){
		if (mass[n][0]){
			creator.clearCoorArray(mass[n]);
			}
			else{
				cubeCoordArray[mass[n].x][mass[n].y][mass[n].z] = null;
				}
		}
	}

creator._insertRay = function(bounding,mouseX,mouseY){
	var worldRay = o3djs.picking.clientPositionToWorldRay(
															mouseX,
															mouseY,
															viewInfo.drawContext,
															cubeClient.width,
															cubeClient.height);
	var rayIntersectionInfo = bounding.intersectRay(worldRay.near,worldRay.far);
	
	return rayIntersectionInfo;
	}
	
creator.cubeMove = function(mass,direction,bounding,mouseX,mouseY){
	var rayIntersectionInfo = creator._insertRay(bounding,mouseX,mouseY);
	var touchX = rayIntersectionInfo.position[0];
	var touchY = rayIntersectionInfo.position[1];
	var touchZ = rayIntersectionInfo.position[2];
 	var deltaCubes;
	
	if (direction[0]){		deltaCubes = Math.round(touchX-freezeSelectMatrix[2][0]-0.5);
		}
	else if(direction[1]){	deltaCubes = Math.round(touchX-freezeSelectMatrix[2][0]+0.5);
			}
	else if(direction[2]){	deltaCubes = Math.round(touchY-freezeSelectMatrix[2][1]-0.5);
			}
	else if(direction[3]){	deltaCubes = Math.round(touchY-freezeSelectMatrix[2][1]+0.5);
			}
	else if(direction[4]){	deltaCubes = Math.round(touchZ-freezeSelectMatrix[2][2]-0.5);
			}
			else{			deltaCubes = Math.round(touchZ-freezeSelectMatrix[2][2]+0.5);
				}
	
	if(deltaCubes != deltaCubesKey[1]){
		
		deltaCubesKey[1] = deltaCubes;
		
		if(!freezeSelectMatrix[3]){
			creator.clearCoorArray(freezeSelectMatrix[0]);
			freezeSelectMatrix[3] = true;
			}
		
			creator._moveSelectMass(direction,deltaCubes);
			mouseButton["moved"] = true; 
/*			deltaCubesKey[2] = deltaCubesKey[2] < deltaCubes ? deltaCubes : deltaCubesKey[2];
 			deltaCubesKey[0] = deltaCubesKey[0]>deltaCubes ? deltaCubes : deltaCubesKey[0];
*/				}
	
	}
	
	
creator._touchCheckX = function(mass,delta){
	if(mass[0]){
		for(var n=0;n < mass.length;n++){
			var touchInfo = creator._touchCheckX(mass[n],delta);
			if(touchInfo){
				return true;
				}
			}
		}
		else{
			createCubeCoordArray(mass.x + delta,mass.y);
			if(	cubeCoordArray[mass.x + delta][mass.y][mass.z]){
				return true;
				}
			}
	}
creator._touchCheckY = function(mass,delta){
	if(mass[0]){
		for(var n=0;n < mass.length;n++){
			var touchInfo = creator._touchCheckY(mass[n],delta);
			if(touchInfo){
				return true;
				}
			}
		}
		else{
			createCubeCoordArray(mass.x,mass.y + delta);
			if(	cubeCoordArray[mass.x][mass.y + delta][mass.z]){
				return true;
				}
			}
	}
creator._touchCheckZ = function(mass,delta){
	if(mass[0]){
		for(var n=0;n < mass.length;n++){
			var touchInfo = creator._touchCheckZ(mass[n],delta);
			if(touchInfo){
				return true;
				}
			}
		}
		else{
			createCubeCoordArray(mass.x,mass.y);
			if(	cubeCoordArray[mass.x][mass.y][mass.z + delta]){
				return true;
				}
			}
	}
	
	
creator._touchReCheckX = function(mass,xBeenCheck){
		var touchInfo = creator._touchCheckX(mass,xBeenCheck);
		if(!touchInfo){
			deltaCubesKey[2] = xBeenCheck;
			return xBeenCheck;
			}
			else if(deltaCubesKey[2] < xBeenCheck){
				xBeenCheck--;
				xBeenCheck = creator._touchReCheckX(mass,xBeenCheck);
				deltaCubesKey[2] = xBeenCheck;
				return xBeenCheck;
				}
				else{
					xBeenCheck++;
					xBeenCheck = creator._touchReCheckX(mass,xBeenCheck);
					deltaCubesKey[2] = xBeenCheck;
					return xBeenCheck;
					}
	}
creator._touchReCheckY = function(mass,yBeenCheck){
		var touchInfo = creator._touchCheckY(mass,yBeenCheck);
		if(!touchInfo){
			deltaCubesKey[2] = yBeenCheck;
			return yBeenCheck;
			}
			else if(deltaCubesKey[2] < yBeenCheck){
				yBeenCheck--;
				yBeenCheck = creator._touchReCheckY(mass,yBeenCheck);
				deltaCubesKey[2] = yBeenCheck;
				return yBeenCheck;
				}
				else{
					yBeenCheck++;
					yBeenCheck = creator._touchReCheckY(mass,yBeenCheck);
					deltaCubesKey[2] = yBeenCheck;
					return yBeenCheck;
					}
	}
creator._touchReCheckZ = function(mass,zBeenCheck){
		var touchInfo = creator._touchCheckZ(mass,zBeenCheck);
		if(!touchInfo){
			deltaCubesKey[2] = zBeenCheck;
			return zBeenCheck;
			}
			else if(deltaCubesKey[2] < zBeenCheck){
				zBeenCheck--;
				zBeenCheck = creator._touchReCheckZ(mass,zBeenCheck);
				deltaCubesKey[2] = zBeenCheck;
				return zBeenCheck;
				}
				else{
					zBeenCheck++;
					zBeenCheck = creator._touchReCheckZ(mass,zBeenCheck);
					deltaCubesKey[2] = zBeenCheck;
					return zBeenCheck;
					}
	}


	
creator._moveSelectMass = function(direction,deltaCubes){
	if (direction[0] || direction[1] ){
		//deltaCubesKey[0]保存真实的可以移动的增量步数。而deltaCubesKey[1]保存的是鼠标的实际增量。
		deltaCubesKey[0] = creator._touchReCheckX(freezeSelectMatrix[0],deltaCubes);
		
		for(var n = 0;n < freezeSelectMatrix[0].length;n++){
			freezeSelectMatrix[0][n].transform.identity();
			freezeSelectMatrix[0][n].transform.translate(freezeSelectMatrix[1][n][0]+deltaCubesKey[0],
															freezeSelectMatrix[1][n][1], 
															freezeSelectMatrix[1][n][2]
															);
			}
	}
	else if (direction[2] || direction[3]){
		//deltaCubesKey[0]保存真实的可以移动的增量步数。而deltaCubesKey[1]保存的是鼠标的实际增量。
		deltaCubesKey[0] = creator._touchReCheckY(freezeSelectMatrix[0],deltaCubes);
		
		for(var n = 0;n < freezeSelectMatrix[0].length;n++){
			freezeSelectMatrix[0][n].transform.identity();
			freezeSelectMatrix[0][n].transform.translate(freezeSelectMatrix[1][n][0],
															freezeSelectMatrix[1][n][1]+deltaCubesKey[0], 
															freezeSelectMatrix[1][n][2]
															);
			}
	}
	else {
		//deltaCubesKey[0]保存真实的可以移动的增量步数。而deltaCubesKey[1]保存的是鼠标的实际增量。
		deltaCubesKey[0] = creator._touchReCheckZ(freezeSelectMatrix[0],deltaCubes);
					
		for(var n = 0;n < freezeSelectMatrix[0].length;n++){
			freezeSelectMatrix[0][n].transform.identity();
			freezeSelectMatrix[0][n].transform.translate(freezeSelectMatrix[1][n][0],
															freezeSelectMatrix[1][n][1], 
															freezeSelectMatrix[1][n][2]+deltaCubesKey[0]
															);
			}
	}
	}	
	
creator.upDateMovedInfo = function(mass,direction){
	for(var n = 0;n < mass.length;n++){
		
		if (direction[0]||direction[1]){
			mass[n].localX = mass[n].transform.localMatrix[3][0];
			mass[n].x = mass[n].transform.worldMatrix[3][0];
			}
		else if (direction[2]||direction[3]){
			mass[n].localY = mass[n].transform.localMatrix[3][1];
			mass[n].y = mass[n].transform.worldMatrix[3][1];
			}
		else{
			mass[n].localZ = mass[n].transform.localMatrix[3][2];
			mass[n].z = mass[n].transform.worldMatrix[3][2];
			}
			
		if (mass[n][0]){
			creator.upDateMovedInfo(mass[n],direction);
			}
			else{
				createCubeCoordArray(mass[n].x,mass[n].y);
				cubeCoordArray[mass[n].x][mass[n].y][mass[n].z] = mass[n];
				}
		}
	}
	

creator.cubeGrowth = function(mass,direction,bounding,mouseX,mouseY){
	var rayIntersectionInfo = creator._insertRay(bounding,mouseX,mouseY);
	var touchX = rayIntersectionInfo.position[0];
	var touchY = rayIntersectionInfo.position[1];
	var touchZ = rayIntersectionInfo.position[2];
 	var deltaCubes;
	
	if (direction[0]){	   		deltaCubes = Math.round(touchX-mass.x-0.5);}
	else if (direction[1]){		deltaCubes = Math.round(mass.x-touchX-0.5);}
	else if (direction[2]){		deltaCubes = Math.round(touchY-mass.y-0.5);}
	else if (direction[3]){		deltaCubes = Math.round(mass.y-touchY-0.5);}
	else if (direction[4]){		deltaCubes = Math.round(touchZ-mass.z-0.5);}
	else{						deltaCubes = Math.round(mass.z-touchZ-0.5);}
	
	if(deltaCubes != deltaCubesKey[1]){
		deltaCubesKey[1] = deltaCubes;
		if (deltaCubes > 0){
			creator.insertSelectCube(freezeSelectNow[0],freezeSelectNow[1],deltaCubes);
			mouseButton["growth"] = true; 
			deltaCubesKey[2] = deltaCubesKey[2] < deltaCubes ? deltaCubes : deltaCubesKey[2];
			}
			else if (deltaCubes < 0){
				mouseButton["growth"] = true; 
				deltaCubesKey[0] = deltaCubesKey[0]>deltaCubes ? deltaCubes : deltaCubesKey[0];
				}
		//首先清除激活线框和清空当前选择，为移动激活框做准备
		creator.removeAllFrames();
		cubeSelectNow = [];
		
		 for (var n=0;n < freezeSelectNow[0].length;n++){
			 var selectCubeX = freezeSelectNow[0][n].x;
			 var selectCubeY = freezeSelectNow[0][n].y;
			 var selectCubeZ = freezeSelectNow[0][n].z;
			 creator._hideSubCube(	deltaCubes+1,deltaCubesKey[2],
										freezeSelectNow[1][n],
										selectCubeX,selectCubeY,selectCubeZ);
			 creator._showSubCube(	deltaCubesKey[0],deltaCubes,
										freezeSelectNow[1][n],
										selectCubeX,selectCubeY,selectCubeZ);
			 _shiftSelect(deltaCubes,freezeSelectNow[1][n],selectCubeX,selectCubeY,selectCubeZ);
			}
		}
	}
	
/*	暂时先不用读取表单的内容
function insertCubeOri(){
	defultRed = parseFloat(document.insertForm.red.value);
	defultGreen = parseFloat(document.insertForm.green.value);
	defultBlue = parseFloat(document.insertForm.blue.value);
	defultAlpha =  parseFloat(document.insertForm.cubeAlpha.value);

	defultValue.x =  parseInt(document.insertForm.x.value);
	defultValue.y = parseInt(document.insertForm.y.value);
	defultValue.z = parseInt(document.insertForm.z.value);
	createCubeCoordArray(defultValue.x,defultValue.y);
	if(!cubeCoordArray[defultValue.x][defultValue.y][defultValue.z]){
		var firstCube = new creator.copyCube(  shape[0].cubeShape,
										defultRed,defultGreen,defultBlue,defultAlpha,
										defultValue.x,defultValue.y,defultValue.z,1);
		firstCube.comAndUpDataBounding();
		cubeSelect[cubeSelect.length-1].push(firstCube);
		}
	}	
*/
creator.insertCubeOri = function(){
	createCubeCoordArray(defultValue.x,defultValue.y);
	if(!cubeCoordArray[defultValue.x][defultValue.y][defultValue.z]){
		var firstCube = new creator.copyCube(  shape[0].cubeShape,
													defultValue.red,
													defultValue.green,
													defultValue.blue,
													defultValue.alpha,
													defultValue.x,defultValue.y,defultValue.z,1);
		firstCube.comAndUpDataBounding();
		cubeSelect[cubeSelect.length-1].push(firstCube);
		}
		else{
			dialog.tips.showBy("原始位置已经有一个方块啦，不能在同一位置生成两个方块，程序还没有改好，期待吧！",400,10);
			
			}
	}	




creator.insertSelectCube = function(mass,direction,bulidNumber){
		   for (var n=0;n < mass.length;n++){
			   creator._bulidCube(mass[n].localX,mass[n].localY,mass[n].localZ,
							mass[n].red,mass[n].green,mass[n].blue,
							mass[n].alpha,
							bulidNumber,direction[n]);
			}
	}

creator.deleUnGrowth = function(){
	if(deltaCubesKey[1] != deltaCubesKey[2]){
		var cubeClearList = new Array;
			 for (var n = 0; n < freezeSelectNow[0].length; n++){
				 var x = freezeSelectNow[0][n].x;
				 var y = freezeSelectNow[0][n].y;
				 var z = freezeSelectNow[0][n].z;
				 
				 if(freezeSelectNow[1][n][0]){
					 for(var num=deltaCubesKey[1]+1;num<=deltaCubesKey[2];num++){
						 if(cubeCoordArray[x+num][y][z]){
						 if(cubeCoordArray[x+num][y][z].parent == cubeSelect[cubeSelect.length-1]){
						 cubeClearList.push(cubeCoordArray[x+num][y][z]);
						 }
						 }
						 }
					 }
				if(freezeSelectNow[1][n][1]){
					 for(var num=deltaCubesKey[1]+1;num<=deltaCubesKey[2];num++){
						 if(cubeCoordArray[x-num][y][z]){
						 if(cubeCoordArray[x-num][y][z].parent == cubeSelect[cubeSelect.length-1]){
						 cubeClearList.push(cubeCoordArray[x-num][y][z]);
						 }
						 }
						 }
					}
				if(freezeSelectNow[1][n][2]){
					 for(var num=deltaCubesKey[1]+1;num<=deltaCubesKey[2];num++){
						 if(cubeCoordArray[x][y+num][z]){
						 if(cubeCoordArray[x][y+num][z].parent == cubeSelect[cubeSelect.length-1]){
						 cubeClearList.push(cubeCoordArray[x][y+num][z]);
						 }
						 }
						 }
					}
				if(freezeSelectNow[1][n][3]){
					 for(var num=deltaCubesKey[1]+1;num<=deltaCubesKey[2];num++){
						 if(cubeCoordArray[x][y-num][z]){
						 if(cubeCoordArray[x][y-num][z].parent == cubeSelect[cubeSelect.length-1]){
						 cubeClearList.push(cubeCoordArray[x][y-num][z]);
						 }
						 }
						 }
					}
				if(freezeSelectNow[1][n][4]){
					 for(var num=deltaCubesKey[1]+1;num<=deltaCubesKey[2];num++){
						 if(cubeCoordArray[x][y][z+num]){
						 if(cubeCoordArray[x][y][z+num].parent == cubeSelect[cubeSelect.length-1]){
						 cubeClearList.push(cubeCoordArray[x][y][z+num]);
						 }
						 }
						 }
					}
				if(freezeSelectNow[1][n][5]){
					 for(var num=deltaCubesKey[1]+1;num<=deltaCubesKey[2];num++){
						 if(cubeCoordArray[x][y][z-num]){
						 if(cubeCoordArray[x][y][z-num].parent == cubeSelect[cubeSelect.length-1]){
						 cubeClearList.push(cubeCoordArray[x][y][z-num]);
						 }
						 }
						 }
					}
				}
			worker.removeCubesXYZ(cubeClearList);
			}
	}

	
creator._bulidCube = function(x,y,z,red,green,blue,cubeAlpha,bulidNumber,direction){
    if(direction[0]){
		var localX = x;
		var localY = y;
		var localZ = z;
		for(var n=0;n < bulidNumber;n++){
			var cubes = new creator.copyCube(shape[0].cubeShape,
													red,green,blue,cubeAlpha,
													localX+=1,localY,localZ,1);
			if(cubes.transform){
				cubeSelect[cubeSelect.length-1].push(cubes);
				}
			}
		}
    if(direction[1]){
		var localX = x;
		var localY = y;
		var localZ = z;
		for(var n=0;n < bulidNumber;n++){
			var cubes = new creator.copyCube(shape[0].cubeShape,
													red,green,blue,cubeAlpha,
													localX+=-1,localY,localZ,1);
			if(cubes.transform){
				cubeSelect[cubeSelect.length-1].push(cubes);
				}
			}
		}
    if(direction[2]){
		var localX = x;
		var localY = y;
		var localZ = z;
		for(var n=0;n < bulidNumber;n++){
			var cubes = new creator.copyCube(shape[0].cubeShape,
													red,green,blue,cubeAlpha,
													localX,localY+=1,localZ,1);
			if(cubes.transform){
				cubeSelect[cubeSelect.length-1].push(cubes);
				}
			}
		}
    if(direction[3]){
		var localX = x;
		var localY = y;
		var localZ = z;
		for(var n=0;n < bulidNumber;n++){
			var cubes = new creator.copyCube(shape[0].cubeShape,
													red,green,blue,cubeAlpha,
													localX,localY+=-1,localZ,1);
			if(cubes.transform){
				cubeSelect[cubeSelect.length-1].push(cubes);
				}
			}
		}
    if(direction[4]){
		var localX = x;
		var localY = y;
		var localZ = z;
		for(var n=0;n < bulidNumber;n++){
			var cubes = new creator.copyCube(shape[0].cubeShape,
													red,green,blue,cubeAlpha,
													localX,localY,localZ+=1,1);
			if(cubes.transform){
				cubeSelect[cubeSelect.length-1].push(cubes);
				}
			}
		}
    if(direction[5]){
		var localX = x;
		var localY = y;
		var localZ = z;
		for(var n=0;n < bulidNumber;n++){
			var cubes = new creator.copyCube(shape[0].cubeShape,
													red,green,blue,cubeAlpha,
													localX,localY,localZ+=-1,1);
			if(cubes.transform){
				cubeSelect[cubeSelect.length-1].push(cubes);
				}
			}
		}
	
	}
	
creator._hideSubCube = function(hideStart,hideEnd,direction,x,y,z){
	var thisCube;
	for (var n = hideStart;n <= hideEnd;n++ ){
		if (direction[0]){
			createCubeCoordArray(x+n,y);
			thisCube = cubeCoordArray[x+n][y][z];
			if (thisCube){
				if(thisCube.parent == cubeSelect[cubeSelect.length-1]){
				thisCube.transform.parent = null;
				}
				}
				}
				
		if(direction[1]){
			createCubeCoordArray(x-n,y);
			thisCube = cubeCoordArray[x-n][y][z];
			if(thisCube){
				if(thisCube.parent == cubeSelect[cubeSelect.length-1]){
				thisCube.transform.parent = null;
				}
				}
				}
				
		if(direction[2]){
			createCubeCoordArray(x,y+n);
			thisCube = cubeCoordArray[x][y+n][z];
			if (thisCube){
				if(thisCube.parent == cubeSelect[cubeSelect.length-1]){
				thisCube.transform.parent = null;
				}
				}
				}
				
		if(direction[3]){
			createCubeCoordArray(x,y-n);
			thisCube = cubeCoordArray[x][y-n][z];
			if (thisCube){
				if(thisCube.parent == cubeSelect[cubeSelect.length-1]){
				thisCube.transform.parent = null;
				}
				}
				}
		if(direction[4]){	
			createCubeCoordArray(x,y);
			thisCube = cubeCoordArray[x][y][z+n];
			if(thisCube){
				if(thisCube.parent == cubeSelect[cubeSelect.length-1]){
				thisCube.transform.parent = null;	
				}
				}
				}
		if(direction[5]){
			createCubeCoordArray(x,y);
			thisCube = cubeCoordArray[x][y][z-n];
			if(thisCube){
				if(thisCube.parent == cubeSelect[cubeSelect.length-1]){
				thisCube.transform.parent = null;
				}
				}
				}
		}
	}
	
creator._showSubCube = function(showStart,showEnd,direction,x,y,z){
	var thisCube;
	for (var n = showStart;n <= showEnd;n++ ){
		if (direction[0]){
			createCubeCoordArray(x+n,y);
			thisCube = cubeCoordArray[x+n][y][z];
			if(thisCube){
				if(thisCube.parent == cubeSelect[cubeSelect.length-1]){
				thisCube.transform.parent = cubeSelect[cubeSelect.length-1].transform;
				}
				}
				}
				
		if(direction[1]){
			createCubeCoordArray(x-n,y);
			thisCube = cubeCoordArray[x-n][y][z];
			if(thisCube){
				if(thisCube.parent == cubeSelect[cubeSelect.length-1]){
				thisCube.transform.parent = cubeSelect[cubeSelect.length-1].transform;
				}
				}
				}
				
		if(direction[2]){
			createCubeCoordArray(x,y+n);
			thisCube = cubeCoordArray[x][y+n][z];
			if(thisCube){
				if(thisCube.parent == cubeSelect[cubeSelect.length-1]){
				thisCube.transform.parent = cubeSelect[cubeSelect.length-1].transform;
				}
				}
				}
		if(direction[3]){
			createCubeCoordArray(x,y-n);
			thisCube = cubeCoordArray[x][y-n][z];
			if(thisCube){
				if(thisCube.parent == cubeSelect[cubeSelect.length-1]){
				thisCube.transform.parent = cubeSelect[cubeSelect.length-1].transform;
				}
				}
				}
		if(direction[4]){
			createCubeCoordArray(x,y);
			thisCube = cubeCoordArray[x][y][z+n];
			if(thisCube){
				if(thisCube.parent == cubeSelect[cubeSelect.length-1]){
				thisCube.transform.parent = cubeSelect[cubeSelect.length-1].transform;
				}
				}
				}
				
		if(direction[5]){
			createCubeCoordArray(x,y);
			thisCube = cubeCoordArray[x][y][z-n];
			if(thisCube){
				if(thisCube.parent == cubeSelect[cubeSelect.length-1]){
				thisCube.transform.parent = cubeSelect[cubeSelect.length-1].transform;
				}
				}
				}
		}
	}
	
	
function _shiftSelect(deltaCubes,direction,x,y,z){
		if(direction[0]){
			var shiftX = x+deltaCubes;
			worker.selectCubeByXYZ(shiftX,y,z,[true,false,false,false,false,false]);
			}
		if(direction[1]){
			var shiftX = x-deltaCubes;
			worker.selectCubeByXYZ(shiftX,y,z,[false,true,false,false,false,false]);
			}
		if(direction[2]){
			var shiftY = y+deltaCubes;
			worker.selectCubeByXYZ(x,shiftY,z,[false,false,true,false,false,false]);
			}
		if(direction[3]){
			var shiftY = y-deltaCubes;
			worker.selectCubeByXYZ(x,shiftY,z,[false,false,false,true,false,false]);
			}
		if(direction[4]){
			var shiftZ = z+deltaCubes;
			worker.selectCubeByXYZ(x,y,shiftZ,[false,false,false,false,true,false]);
			}
		if(direction[5]){
			var shiftZ = z-deltaCubes;
			worker.selectCubeByXYZ(x,y,shiftZ,[false,false,false,false,false,true]);
			}
		}
//------------------------------------------------------------------------------------------------------------


creator.addFrames = function(	frameShape,planShape,
									colorRed,colorGreen,colorBlue,frameAlpha,
									selectCube,directionArray,scaleL){
	if (selectCube[0]){
		for(var n=0;n < selectCube.length;n++){
			creator.addFrames(frameShape,planShape,
								   colorRed,colorGreen,colorBlue,frameAlpha,
								   selectCube[n],directionArray,scaleL);
			}
		}
		else{
			selectCube.wireFrame = new creator.copyFramesMass(	frameShape,planShape,
																	colorRed,colorGreen,colorBlue,frameAlpha,
																	selectCube,directionArray,scaleL);
			}
	}
	
creator.copyFramesMass = function(	frameShape,planShape,
										colorRed,colorGreen,colorBlue,frameAlpha,
										selectCube,directionArray,scaleL){
	
		this.red = colorRed;
		this.green = colorGreen;
		this.blue = colorBlue;
		this.alpha = frameAlpha;
		this.parent = selectCube;
		
		this.transform = cubePack.createObject("Transform");
		this.transform.parent = selectCube.transform;
		this.transform.addShape(frameShape);
		this.transform.scale(scaleL,
							 scaleL,
							 scaleL);
		
		if(directionArray){
		var rad = PI/2;
		this.transformPlanArray = new Array;
			if(directionArray[0]){
				var newPlan = this._addPlan([0,rad,0],[0.02,0,0],planShape,this.transform);
				this.transformPlanArray[0] = newPlan;
				}
			if(directionArray[1]){
				var newPlan = this._addPlan([0,-rad,0],[-0.02,0,0],planShape,this.transform);
				this.transformPlanArray[1] = newPlan;
				}
			if(directionArray[2]){
				var newPlan = this._addPlan([-rad,0,0],[0,0.02,0],planShape,this.transform);
				this.transformPlanArray[2] = newPlan;
				}
			if(directionArray[3]){
				var newPlan = this._addPlan([rad,0,0],[0,-0.02,0],planShape,this.transform);
				this.transformPlanArray[3] = newPlan;
				}
			if(directionArray[4]){
				var newPlan = this._addPlan([0,0,0],[0,0,0.02],planShape,this.transform);
				this.transformPlanArray[4] = newPlan;
				}
			if(directionArray[5]){
				var newPlan = this._addPlan([0,PI,0],[0,0,-0.02],planShape,this.transform);
				this.transformPlanArray[5] = newPlan;
				}
			}
	}

creator.copyFramesMass.prototype._addPlan = function (selectDir,selectTra,
															 planShape,parent){
	var transform = cubePack.createObject("Transform");
	transform.parent = parent;
	transform.addShape(planShape);
	transform.translate(selectTra);
	transform.rotateZYX(selectDir);	
	transform.createParam("diffuse","ParamFloat4").value = [1,1,1,1];
	return transform
	}

	
creator.removeAllFrames = function(){
	if(cubeSelectNow[0]){
		for(var n = 0;n < cubeSelectNow.length;n++){
			creator.clearWireFrame(n);
			}
		}
	}
	
creator.clearWireFrame = function(num){
	if(cubeSelectNow[num] && cubeSelectNow[num][2][0]){
		var subClearMass = cubeSelectNow[num][2];
		for(var n=0;n < subClearMass.length;n++){
			creator._clearFrames(subClearMass[n]);
			}
		}
		else if(cubeSelectNow[num] && cubeSelectNow[num][2].wireFrame){
				cubeSelectNow[num][2].wireFrame.transform.parent = null;
				cubeSelectNow[num][2].wireFrame = null;
				}
	}	



creator._clearFrames = function(mass){
	if(mass[0]){
		for(var n=0;n < mass.length;n++){
			creator._clearFrames(mass[n]);
			}
		}else if(mass.wireFrame){
			mass.wireFrame.transform.parent = null;
			mass.wireFrame = null;
			}
	}

	
creator.frameLine = function(red,green,blue,alpha){
	this.effect = cubePack.createObject("Effect");
	this.shaders = o3djs.debug.createColorShaders_(O3D_DEBUG_COLOR_PARAM_NAME);
	this.effect.loadFromFXString(this.shaders);
	
	this.material = cubePack.createObject("Material");
	this.material.effect = this.effect;
	this.material.drawList = viewInfo.zOrderedDrawList;
	this.effect.createUniformParameters(this.material);
	
	this.material.getParam(O3D_DEBUG_COLOR_PARAM_NAME).value = [red,green,blue,alpha];
	this.color = this.material.getParam(O3D_DEBUG_COLOR_PARAM_NAME);
	var a = 1/2;
	var verticesArray = [
						 -a, +a, +a,
						 +a, +a, +a,
						 -a, -a, +a,
						 +a, -a, +a,
						 -a, +a, -a,
						 +a, +a, -a,
						 -a, -a, -a,
						 +a, -a, -a
						 ];
	var indicesArray = [
						0,1,
						1,3,
						3,2,
						2,0,
						0,4,
						1,5,
						3,7,
						2,6,
						4,5,
						5,7,
						7,6,
						6,4
						];
	
	this.streamBank = cubePack.createObject("StreamBank");
	this.primitive = cubePack.createObject("Primitive");
	this.frameShape = cubePack.createObject("Shape");	
	this.vertexBuffer = cubePack.createObject("VertexBuffer");
	
	this.positionField = this.vertexBuffer.createField("FloatField",3);
	this.vertexBuffer.set(verticesArray);
	
	this.primitive.owner = this.frameShape
	this.primitive.createDrawElement(cubePack,null);
	this.primitive.streamBank = this.streamBank;
	this.primitive.material = this.material;
	this.primitive.numberVertices = 8;
	this.primitive.numberPrimitives = 12;
	this.primitive.primitiveType = cubeO3d.Primitive.LINELIST;
	
	this.streamBank.setVertexStream(cubeO3d.Stream.POSITION,
									0,
									this.positionField,
									0);
	this.indexBuffer = cubePack.createObject("IndexBuffer");
	this.indexBuffer.set(indicesArray);
	this.primitive.indexBuffer = this.indexBuffer;
	
	}
	
	
creator.framePlan = function(){
	this.planMaterial = cubePack.createObject("Material");
	this.planMaterial.drawList = viewInfo.zOrderedDrawList; 
	this.planMaterial.createParam("diffuse","ParamFloat4").value = [1,1,1,1];
	
	this.planMaterial.createParam('emissive', 'ParamFloat4').value = [0, 0, 0, 1]; 
	this.planMaterial.createParam('ambient', 'ParamFloat4').value = [1, 1, 1, 1];
	this.planMaterial.createParam('specular', 'ParamFloat4').value = [1, 1, 1, 1];
	this.planMaterial.createParam('shininess', 'ParamFloat').value = 50;
	this.planMaterial.createParam('specularFactor', 'ParamFloat').value = 1;
	
	this.planMaterial.createParam('lightColor', 'ParamFloat4').value = [1, 1, 1, 1]; 
	this.lightPositionParam = this.planMaterial.createParam('lightWorldPos', 'ParamFloat3'); 
	this.lightPositionParam.value = eyePosi;
	
	this.record = o3djs.effect.buildStandardShaderString(this.planMaterial,"lambert"); 
	this.effect = cubePack.createObject("Effect");
	this.effect.name = this.record.description;
	this.effect.loadFromFXString(this.record.shader);
	this.planMaterial.effect = this.effect;
	this.effect.createUniformParameters(this.planMaterial); 

	var a = 1/2;
	this.positionArray = [  -a, +a, +a,
 						     +a, +a, +a,
							 -a, -a, +a,
     						 +a, -a, +a
							 ];
	
	this.normalArray = [	    0, 0, 1,
								0, 0, 1,
								0, 0, 1,
								0, 0, 1
								];

	this.indicesArray = [	  0, 2, 1,
							  2, 3, 1
							  ];

	this.planShape = cubePack.createObject("Shape");
	this.planPrimitive = cubePack.createObject("Primitive");
	this.streamBank = cubePack.createObject("StreamBank");
	
	this.planPrimitive.material = this.planMaterial;
	this.planPrimitive.owner = this.planShape;
	this.planPrimitive.streamBank = this.streamBank;
	
	this.planPrimitive.primitiveType = cubeO3d.Primitive.TRIANGLELIST;
	this.planPrimitive.numberPrimitives = 2;
	this.planPrimitive.numberVertices = 4;
	this.planPrimitive.createDrawElement(cubePack,null);
	
	this.positionBuffer = cubePack.createObject("VertexBuffer");
	this.positionField = this.positionBuffer.createField("FloatField",3);
	this.streamBank.setVertexStream(
									cubeO3d.Stream.POSITION,
									0,
									this.positionField,
									0);
	this.normalField = this.positionBuffer.createField("FloatField",3);
	this.streamBank.setVertexStream(
									cubeO3d.Stream.NORMAL,
									0,
									this.normalField,
									0);
 
    this.positionBuffer.allocateElements(4); 	
	
	this.positionField.setAt(0,this.positionArray);
	this.normalField.setAt(0,this.normalArray);
	
	this.indexBuffer = cubePack.createObject("IndexBuffer");
	this.indexBuffer.set(this.indicesArray);
	this.planPrimitive.indexBuffer = this.indexBuffer;
	
//	this.planTransform = cubePack.createObject("Transform");
//	this.planTransform.addShape(this.planShape);
//	this.planTransform.parent = cubeClient.root;
	}
	
creator.framePlan.prototype.updataLight = function(){
	this.lightPositionParam.value = eyePosi;
	}
	
	
	
creator.copyCube = function(cubeShape,colorRed,colorGreen,colorBlue,cubeAlpha,localX,localY,localZ,localL){
	
	var parent = cubeSelect[cubeSelect.length-1];
	var worldX = parent.x+localX;
	var worldY = parent.y+localY;
	var worldZ = parent.z+localZ;
	
	createCubeCoordArray(worldX, worldY);
    if (!cubeCoordArray[worldX][worldY][worldZ]){
		this.red = colorRed;
		this.green = colorGreen;
		this.blue = colorBlue;
		this.alpha = cubeAlpha;
		this.x = worldX;
		this.y = worldY;
		this.z = worldZ;
		
		this.l = localL
		this.localX = localX;
		this.localY = localY;
		this.localZ = localZ;
		
		this.parent = cubeSelect[cubeSelect.length-1];
		this.transform = cubePack.createObject("Transform");
		this.transform.parent = cubeSelect[cubeSelect.length-1].transform;
		this.transform.addShape(cubeShape);
		this.transform.translate(localX,
								 localY,
								 localZ);
		this.transform.scale(localL,
							 localL,
							 localL);
		this.transform.createParam("diffuse","ParamFloat4").value = [
																	 this.red,
																	 this.green,
																	 this.blue,
																	 this.alpha
																	 ];
		cubeCoordArray[worldX][worldY][worldZ] = this;
		}
	}
creator.copyCube.prototype.upDataCoor = function (){
	this.upDataX();
	this.upDataY();
	this.upDataZ();
	//this.upDataL();
	this.upDataLocalX();
	this.upDataLocalY();
	this.upDataLocalZ();
	//this.upDataLocalL();
	}
	
creator.copyCube.prototype.upDataX = function (){
	this.x = this.transform.worldMatrix[3][0];
	}
creator.copyCube.prototype.upDataY = function (){
	this.y = this.transform.worldMatrix[3][1];
	}
creator.copyCube.prototype.upDataZ = function (){
	this.z = this.transform.worldMatrix[3][2];
	}
creator.copyCube.prototype.upDataL = function (){
	this.l = this.transform.worldMatrix[0][0];
	}
creator.copyCube.prototype.upDataLocalX = function (){
	this.localX = this.transform.localMatrix[3][0];
	}
creator.copyCube.prototype.upDataLocalY = function (){
	this.localY = this.transform.localMatrix[3][1];
	}
creator.copyCube.prototype.upDataLocalZ = function (){
	this.localZ = this.transform.localMatrix[3][2];
	}
creator.copyCube.prototype.upDataLocalL = function (){
	this.localL = this.transform.localMatrix[0][0];
	}
creator.copyCube.prototype.comBounding = function(){
	this.boundingBox = this.transform.shapes[0].elements[0].getBoundingBox(0).mul(this.transform.worldMatrix);
	}
creator.copyCube.prototype.comAndUpDataBounding = function(){
	//不建议多个cube连续使用时运行这个函数来更新。
	this.boundingBox = this.transform.shapes[0].elements[0].getBoundingBox(0).mul(this.transform.worldMatrix);
	creator.upDataBoundingTree(this);
	}
	
creator.copyCube.prototype.changeColor = function(r,g,b,alpha){
	this.red = r;
	this.green = g;
	this.blue = b;
	this.alpha = alpha;
	this.transform.getParam("diffuse").value = [r,g,b,alpha];
	//色彩统计colorOftenUse[n]的定义：[[red,green,blue,alpha],num]
	var red = Math.round(r*255);
	var green = Math.round(g*255);
	var blue = Math.round(b*255);
	var alpha = Math.round(alpha*255);
	var used;
	for(var n=0;n < colorOftenUse.length;n++){
		if(	red==colorOftenUse[n][0][0] &&
			green==colorOftenUse[n][0][1] &&
			blue==colorOftenUse[n][0][2] &&
			alpha==colorOftenUse[n][0][3]){
											colorOftenUse[n][1]++;
											used = true;
											if(n && colorOftenUse[n][1]>colorOftenUse[n-1][1]){
												var tran = colorOftenUse[n-1];
												colorOftenUse[n-1] = colorOftenUse[n];
												colorOftenUse[n] = tran;
												}
											}
		}
	if(!used){
		colorOftenUse.push([[red,green,blue,alpha],1]);
		}
				var findFrames = totalColorUnitFrame.findFrameByName(["colorOftenFrame"]);
				var theLibrary = findFrames[0].findFrameByName(["colorOften"]);
			
				if(colorOftenUse.length != theLibrary[0].child.length){
					var ii = (colorOftenUse.length-1 - (colorOftenUse.length-1)%3)/3;
					if(colorOftenUse.length<=9){	
						var colorBlock = dialog.createColorBlock();
						theLibrary[0].addFrame([colorBlock,(colorOftenUse.length-1)%3*25+1,ii*25+1]);
						}
					}
				for(var k=0;k < colorOftenUse.length && k < 9;k++){
					theLibrary[0].child[k].changeBgColor([colorOftenUse[k][0][0]/255,
														  colorOftenUse[k][0][1]/255,
														  colorOftenUse[k][0][2]/255,
														  colorOftenUse[k][0][3]/255]);
										}
		
	}

creator.upDataBoundingTree = function(mass){
	//仅仅为 添加cube使用的bounding依次向上更新的函数。
	if (mass.parent != cubeMass){
		mass.parent.boundingBox = 	mass.parent.boundingBox ? 
									mass.parent.boundingBox.add(mass.boundingBox) :
									mass.boundingBox;
		creator.upDataBoundingTree(mass.parent);
		}
	}
	
creator.oriCube = function(){
	this.cubeMaterial = cubePack.createObject("Material");
	this.cubeMaterial.drawList = viewInfo.zOrderedDrawList; 
	this.cubeMaterial.createParam("diffuse","ParamFloat4").value = [1,1,1,1];
	
	this.cubeMaterial.createParam('emissive', 'ParamFloat4').value = [0, 0, 0, 1]; 
	this.cubeMaterial.createParam('ambient', 'ParamFloat4').value = [0, 0, 0, 1];
	this.cubeMaterial.createParam('specular', 'ParamFloat4').value = [1, 1, 1, 1];
	this.cubeMaterial.createParam('shininess', 'ParamFloat').value = 100;
	this.cubeMaterial.createParam('specularFactor', 'ParamFloat').value = 0.25;
	
	this.cubeMaterial.createParam('lightColor', 'ParamFloat4').value = [1, 1, 1, 1]; 
	this.lightPositionParam = this.cubeMaterial.createParam('lightWorldPos', 'ParamFloat3'); 
	this.lightPositionParam.value = eyePosi;
	
	this.record = o3djs.effect.buildStandardShaderString(this.cubeMaterial,"phong"); 
	this.effect = cubePack.createObject("Effect");
	this.effect.name = this.record.description;
	this.effect.loadFromFXString(this.record.shader);
	this.cubeMaterial.effect = this.effect;
	this.effect.createUniformParameters(this.cubeMaterial); 

	var a = 1/2;
	this.positionArray = [	 -a, +a, +a,
 						     +a, +a, +a,
							 -a, -a, +a,
     						 +a, -a, +a,
 						     +a, +a, +a,
 						     +a, +a, -a,
 						     +a, -a, +a,
 						     +a, -a, -a,
 						     +a, +a, -a,
 						     -a, +a, -a,
 						     +a, -a, -a,
 						     -a, -a, -a,
 						     -a, +a, -a,
 						     -a, +a, +a,
 						     -a, -a, -a,
 						     -a, -a, +a,
 						     -a, +a, -a,
 						     +a, +a, -a,
 						     -a, +a, +a,
 						     +a, +a, +a,
 						     -a, -a, +a,
 						     +a, -a, +a,
 						     -a, -a, -a,
 						     +a, -a, -a
							 ];
	
	this.normalArray = [	    0, 0, 1,
								0, 0, 1,
								0, 0, 1,
								0, 0, 1,
								1, 0, 0,
								1, 0, 0,
								1, 0, 0,
								1, 0, 0,
								0, 0,-1,
								0, 0,-1,
								0, 0,-1,
								0, 0,-1,
							   -1, 0, 0,
							   -1, 0, 0,
							   -1, 0, 0,
							   -1, 0, 0,
							    0, 1, 0,
							    0, 1, 0,
							    0, 1, 0,
							    0, 1, 0,
							    0,-1, 0,
							    0,-1, 0,
							    0,-1, 0,
							    0,-1, 0
								];

	this.indicesArray = [	  0, 2, 1,
							  2, 3, 1,
							  4, 6, 5, 
							  6, 7, 5,
							  8, 10,9,
							  10,11,9,
							  12,14,13, 
							  14,15,13,
							  16,18,17, 
							  18,19,17, 
							  20,22,21, 
							  22,23,21
							  ];
	
	this.cubeShape = cubePack.createObject("Shape");
	this.cubePrimitive = cubePack.createObject("Primitive");
	this.streamBank = cubePack.createObject("StreamBank");
	
	this.cubePrimitive.material = this.cubeMaterial;
	this.cubePrimitive.owner = this.cubeShape;
	this.cubePrimitive.streamBank = this.streamBank;
	
	this.cubePrimitive.primitiveType = cubeO3d.Primitive.TRIANGLELIST;
	this.cubePrimitive.numberPrimitives = 12;
	this.cubePrimitive.numberVertices = 24;
	this.cubePrimitive.createDrawElement(cubePack,null);
	
	this.positionBuffer = cubePack.createObject("VertexBuffer");
	this.positionField = this.positionBuffer.createField("FloatField",3);
	this.streamBank.setVertexStream(
									cubeO3d.Stream.POSITION,
									0,
									this.positionField,
									0);
	this.normalField = this.positionBuffer.createField("FloatField",3);
	this.streamBank.setVertexStream(
									cubeO3d.Stream.NORMAL,
									0,
									this.normalField,
									0);
 
    this.positionBuffer.allocateElements(24); 	
	
	this.positionField.setAt(0,this.positionArray);
	this.normalField.setAt(0,this.normalArray);
	
	this.indexBuffer = cubePack.createObject("IndexBuffer");
	this.indexBuffer.set(this.indicesArray);
	this.cubePrimitive.indexBuffer = this.indexBuffer;
	
	//this.cubeTransform = cubePack.createObject("Transform");
	//this.cubeTransform.addShape(this.cubeShape);
	//this.cubeTransform.parent = cubeClient.root;
	}

creator.oriCube.prototype.updataLight = function(){
	 this.lightPositionParam.value = eyePosi;
	}
	


creator.coupleArrow = function(red,green,blue,alpha){
	this.arrowMaterial = cubePack.createObject("Material");
	this.arrowMaterial.drawList = viewInfo.zOrderedDrawList; 
	this.arrowMaterial.createParam("diffuse","ParamFloat4").value = [1,1,1,1];
	
	this.arrowMaterial.createParam('emissive', 'ParamFloat4').value = [0, 0, 0, 1]; 
	this.arrowMaterial.createParam('ambient', 'ParamFloat4').value = [0, 0, 0, 1];
	this.arrowMaterial.createParam('specular', 'ParamFloat4').value = [1, 1, 1, 1];
	this.arrowMaterial.createParam('shininess', 'ParamFloat').value = 100;
	this.arrowMaterial.createParam('specularFactor', 'ParamFloat').value = 1;
	
	this.arrowMaterial.createParam('lightColor', 'ParamFloat4').value = [1, 1, 1, 1]; 
	this.lightPositionParam = this.arrowMaterial.createParam('lightWorldPos', 'ParamFloat3'); 
	this.lightPositionParam.value = eyePosi;
	
	this.record = o3djs.effect.buildStandardShaderString(this.arrowMaterial,"phong"); 
	this.effect = cubePack.createObject("Effect");
	this.effect.name = this.record.description;
	this.effect.loadFromFXString(this.record.shader);
	this.arrowMaterial.effect = this.effect;
	this.effect.createUniformParameters(this.arrowMaterial); 
	
	var lineL = 1;
	var arrowL = 0.618;
	var overL = 0.2;
	
	this.arrowShape = o3djs.primitives.createTruncatedCone(
										cubePack,
										this.arrowMaterial,
										0.1,   // Bottom radius.
										0.0,   // Top radius.
										arrowL,   // Height.
										16,    // Number of radial subdivisions.
										10);   // Number of vertical subdivisions.
	this.lineShape = o3djs.primitives.createCylinder(
										cubePack,
										this.arrowMaterial,
										0.02,   // Radius.
										lineL+overL,   // Height.
										16,    // Number of radial subdivisions.
										10);   // Number of vertical subdivisions.
	//为了简化操作，这一步的transform专门用于移动。
	this.transform = cubePack.createObject("Transform");
	this.transform.parent = null;
	//为了简化操作，这一步的transform专门用于旋转，平移。
	this.transformRot = cubePack.createObject("Transform");
	this.transformRot.parent = this.transform;
	//第一个箭头
	this.transform_0 = cubePack.createObject("Transform");
	this.transform_0.parent = this.transformRot;
	
	this.arrowTransform_0_Move = cubePack.createObject("Transform");
	this.arrowTransform_0_Move.parent = this.transform_0;
	
	this.arrowTransform_0 = cubePack.createObject("Transform");
	this.arrowTransform_0.parent = this.arrowTransform_0_Move;
	this.arrowTransform_0.addShape(this.arrowShape);
	this.arrowTransform_0.createParam("diffuse","ParamFloat4").value = [red,green,blue,alpha];
	this.arrowTransform_0.translate(0,-lineL-arrowL/2-1,0);
	this.arrowTransform_0.rotateZ(PI);
	
	this.lineTransform_0 = cubePack.createObject("Transform");
	this.lineTransform_0.parent = this.transform_0;
	this.lineTransform_0.addShape(this.lineShape);
	this.lineTransform_0.createParam("diffuse","ParamFloat4").value = [red,green,blue,alpha];
	this.lineTransform_0.translate(0,-(lineL+overL)/2-1,0);
	this.lineTransform_0.rotateZ(PI);
	
	//第二个箭头
	this.transform_1 = cubePack.createObject("Transform");
	this.transform_1.parent = this.transformRot;
	
	this.arrowTransform_1_Move = cubePack.createObject("Transform");
	this.arrowTransform_1_Move.parent = this.transform_1;

	this.arrowTransform_1 = cubePack.createObject("Transform");
	this.arrowTransform_1.parent = this.arrowTransform_1_Move;
	this.arrowTransform_1.addShape(this.arrowShape);
	this.arrowTransform_1.createParam("diffuse","ParamFloat4").value = [red,green,blue,alpha];
	this.arrowTransform_1.translate(0,lineL+arrowL/2,0);
	
	this.lineTransform_1 = cubePack.createObject("Transform");
	this.lineTransform_1.parent = this.transform_1;
	this.lineTransform_1.addShape(this.lineShape);
	this.lineTransform_1.createParam("diffuse","ParamFloat4").value = [red,green,blue,alpha];
	this.lineTransform_1.translate(0,(lineL+overL)/2,0);
	}
	
creator.coupleArrow.prototype.updataLight = function(){
	 this.lightPositionParam.value = eyePosi;
	}

creator.coupleArrow.prototype.clear = function(parent){
	this.visible = false;
	this.transform.parent = null;
	}
	
creator.coupleArrow.prototype.moveByPick = function(pickInfo,delta){
	this.transform.identity();
	var deltaXYZ = pickInfo[1].position;
	if(pickInfo[3][0]){			deltaXYZ[0]+=delta;}
	else if(pickInfo[3][1]){	deltaXYZ[0]+=delta;}
	else if(pickInfo[3][2]){	deltaXYZ[1]+=delta;}
	else if(pickInfo[3][3]){	deltaXYZ[1]+=delta;}
	else if(pickInfo[3][4]){	deltaXYZ[2]+=delta;}
	else{						deltaXYZ[2]+=delta;}
	this.transform.translate(deltaXYZ[0],deltaXYZ[1],deltaXYZ[2]);
	}
	
creator.coupleArrow.prototype.displayByPick = function(pickInfo,includeAll){
	this.visible = true;
			this.transform.parent = cubeMass.transform;
			this.transform.identity();
			this.transformRot.identity();
			this.transform_0.identity();
			this.transform_1.identity();
			
			
			//求出这个组中对应点击方块的箭头偏移量，可以直接被transform_0使用，是一个负值。
			if(includeAll){
				var allSelected = [];
				for(var n=0;n < cubeSelectNow.length;n++){
					allSelected.push(cubeSelectNow[n][2]);
					}
				var deltaL = this._findFarCube(allSelected,pickInfo[4],pickInfo[3]);
				}
			else{
				var deltaL = this._findFarCube(pickInfo[2],pickInfo[4],pickInfo[3]);
				}
			this.transform_0.translate(0,deltaL,0);
			
			var deltaXYZ = pickInfo[1].position;
			this.transform.translate(deltaXYZ[0],deltaXYZ[1],deltaXYZ[2]);
			
			if(pickInfo[3][0]){
				this.transformRot.rotateZ(-PI/2);
				}
			else if(pickInfo[3][1]){
				this.transformRot.rotateZ(PI/2);
				}
			else if(pickInfo[3][3]){
				this.transformRot.rotateZ(PI);
				}
			else if(pickInfo[3][4]){
				this.transformRot.rotateX(PI/2);
				}
			else if(pickInfo[3][5]){
				this.transformRot.rotateX(-PI/2);
				}
	}

creator.coupleArrow.prototype._findFarCube = function(mass,cube,direction){
	if(direction[0]){		return this._findFarCube_X(mass,cube.y,cube.z)-cube.x;}
	else if(direction[1]){	return cube.x-this._findFarCube_nX(mass,cube.y,cube.z);}
	else if(direction[2]){	return this._findFarCube_Y(mass,cube.x,cube.z)-cube.y;}
	else if(direction[3]){	return cube.y-this._findFarCube_nY(mass,cube.x,cube.z);}
	else if(direction[4]){	return this._findFarCube_Z(mass,cube.x,cube.y)-cube.z;}
	else{					return cube.z-this._findFarCube_nZ(mass,cube.x,cube.y);}
	
	}

creator.coupleArrow.prototype._findFarCube_X = function(mass,y,z){
	var value;
	var temp;
	for(var n=0;n < mass.length;n++){
			if(mass[n][0]){
				temp = this._findFarCube_X(mass[n],y,z);
				value = value==undefined ? temp : value;
				value = value>temp ? temp : value;
				}
				else if(mass[n].y==y && mass[n].z==z){
						value = value==undefined ? mass[n].x : value;
						value = value>mass[n].x ? mass[n].x : value;
						}
		}
	return value;
	}
	
creator.coupleArrow.prototype._findFarCube_nX = function(mass,y,z){
	var value;
	var temp;
	for(var n=0;n < mass.length;n++){
			if(mass[n][0]){
				temp = this._findFarCube_nX(mass[n],y,z);
				value = value==undefined ? temp : value;
				value = value < temp ? temp : value;
				}
				else if(mass[n].y==y && mass[n].z==z){
						value = value==undefined ? mass[n].x : value;
						value = value < mass[n].x ? mass[n].x : value;
						}
		}
	return value;
	}
	
creator.coupleArrow.prototype._findFarCube_Y = function(mass,x,z){
	var value;
	var temp;
	for(var n=0;n < mass.length;n++){
			if(mass[n][0]){
				temp = this._findFarCube_Y(mass[n],x,z);
				value = value==undefined ? temp : value;
				value = value>temp ? temp : value;
				}
				else if(mass[n].x==x && mass[n].z==z){
						value = value==undefined ? mass[n].y : value;
						value = value>mass[n].y ? mass[n].y : value;
						}
		}
	return value;
	}
creator.coupleArrow.prototype._findFarCube_nY = function(mass,x,z){
	var value;
	var temp;
	for(var n=0;n < mass.length;n++){
			if(mass[n][0]){
				temp = this._findFarCube_nY(mass[n],x,z);
				value = value==undefined ? temp : value;
				value = value < temp ? temp : value;
				}
				else if(mass[n].x==x && mass[n].z==z){
						value = value==undefined ? mass[n].y : value;
						value = value < mass[n].y ? mass[n].y : value;
						}
		}
	return value;
	}
creator.coupleArrow.prototype._findFarCube_Z = function(mass,x,y){
	var value;
	var temp;
	for(var n=0;n < mass.length;n++){
			if(mass[n][0]){
				temp = this._findFarCube_Z(mass[n],x,y);
				value = value==undefined ? temp : value;
				value = value>temp ? temp : value;
				}
				else if(mass[n].x==x && mass[n].y==y){
						value = value==undefined ? mass[n].z : value;
						value = value>mass[n].z ? mass[n].z : value;
						}
		}
	return value;
	}
creator.coupleArrow.prototype._findFarCube_nZ = function(mass,x,y){
	var value;
	var temp;
	for(var n=0;n < mass.length;n++){
			if(mass[n][0]){
				temp = this._findFarCube_nZ(mass[n],x,y);
				value = value==undefined ? temp : value;
				value = value < temp ? temp : value;
				}
				else if(mass[n].x==x && mass[n].y==y){
						value = value==undefined ? mass[n].z : value;
						value = value < mass[n].z ? mass[n].z : value;
						}
		}
	return value;
	}


creator.growthArrow = function(red,green,blue,alpha){
	
	
	this.arrowMaterial = cubePack.createObject("Material");
	this.arrowMaterial.drawList = viewInfo.zOrderedDrawList; 
	this.arrowMaterial.createParam("diffuse","ParamFloat4").value = [1,1,1,1];
	
	this.arrowMaterial.createParam('emissive', 'ParamFloat4').value = [0, 0, 0, 1]; 
	this.arrowMaterial.createParam('ambient', 'ParamFloat4').value = [0, 0, 0, 1];
	this.arrowMaterial.createParam('specular', 'ParamFloat4').value = [1, 1, 1, 1];
	this.arrowMaterial.createParam('shininess', 'ParamFloat').value = 100;
	this.arrowMaterial.createParam('specularFactor', 'ParamFloat').value = 1;
	
	this.arrowMaterial.createParam('lightColor', 'ParamFloat4').value = [1, 1, 1, 1]; 
	this.lightPositionParam = this.arrowMaterial.createParam('lightWorldPos', 'ParamFloat3'); 
	this.lightPositionParam.value = eyePosi;
	
	this.record = o3djs.effect.buildStandardShaderString(this.arrowMaterial,"phong"); 
	this.effect = cubePack.createObject("Effect");
	this.effect.name = this.record.description;
	this.effect.loadFromFXString(this.record.shader);
	this.arrowMaterial.effect = this.effect;
	this.effect.createUniformParameters(this.arrowMaterial); 
	
	var lineL = 0.5;
	var arrowL = 0.5;
	var cubeL = 0.20;
	var overL = 0.03;
	
	this.arrowShape = o3djs.primitives.createTruncatedCone(
														cubePack,
														this.arrowMaterial,
														0.1,   // Bottom radius.
														0.0,   // Top radius.
														arrowL,   // Height.
														16,    // Number of radial subdivisions.
														10);   // Number of vertical subdivisions.
	this.lineShape = o3djs.primitives.createCylinder(
													cubePack,
													this.arrowMaterial,
													0.02,   // Radius.
													lineL,   // Height.
													16,    // Number of radial subdivisions.
													10);   // Number of vertical subdivisions.
	
	
	this.cubeShape = o3djs.primitives.createCube(
												 cubePack,
												 this.arrowMaterial,
												 cubeL);
	//为了简化操作，这一步的transform专门用于移动。
	this.transform = cubePack.createObject("Transform");
	this.transform.parent = null;
	//为了简化操作，这一步的transform专门用于旋转,放大。
	this.transformRot = cubePack.createObject("Transform");
	this.transformRot.parent = this.transform;
	//双向箭头
	this.transform_0 = cubePack.createObject("Transform");
	this.transform_0.parent = this.transformRot;
		//向外的箭头
		this.arrowTransform_0 = cubePack.createObject("Transform");
		this.arrowTransform_0.parent = this.transform_0;
		this.arrowTransform_0.addShape(this.arrowShape);
		this.arrowTransform_0.createParam("diffuse","ParamFloat4").value = [red,green,blue,alpha];
		this.arrowTransform_0.translate(0,overL+arrowL*1.5+lineL,0);
		
		//向里的箭头
		this.arrowTransform_1 = cubePack.createObject("Transform");
		this.arrowTransform_1.parent = this.transform_0;
		this.arrowTransform_1.addShape(this.arrowShape);
		this.arrowTransform_1.createParam("diffuse","ParamFloat4").value = [red,green,blue,alpha];
		this.arrowTransform_1.translate(0,overL+arrowL/2,0);
		this.arrowTransform_1.rotateZ(PI);
		//线条
		this.lineTransform = cubePack.createObject("Transform");
		this.lineTransform.parent = this.transform_0;
		this.lineTransform.addShape(this.lineShape);
		this.lineTransform.createParam("diffuse","ParamFloat4").value = [red,green,blue,alpha];
		this.lineTransform.translate(0,overL+arrowL+lineL/2,0);
	//线条中的方块
	this.cubeTransform = cubePack.createObject("Transform");
	this.cubeTransform.parent = null;
	this.cubeTransform.addShape(this.cubeShape);
	this.cubeTransform.createParam("diffuse","ParamFloat4").value = [red,green,blue,alpha];
	this.cubeTransform.translate(0,cubeL/2+overL,0);
	}

creator.growthArrow.prototype.displayCube = function(){
	this.cubeTransform.parent = this.transformRot;
	}
creator.growthArrow.prototype.clearCube = function(){
	this.cubeTransform.parent = null;
	}

creator.growthArrow.prototype.displayByPick = function(pickInfo){
	this.visible = true;
			this.transform.parent = cubeMass.transform;
			this.transform.identity();
			this.transformRot.identity();
			
			var deltaXYZ = pickInfo[1].position;
			this.transform.translate(deltaXYZ[0],deltaXYZ[1],deltaXYZ[2]);
			
			if(pickInfo[3][0]){
				this.transformRot.rotateZ(-PI/2);
				}
			else if(pickInfo[3][1]){
				this.transformRot.rotateZ(PI/2);
				}
			else if(pickInfo[3][3]){
				this.transformRot.rotateZ(PI);
				}
			else if(pickInfo[3][4]){
				this.transformRot.rotateX(PI/2);
				}
			else if(pickInfo[3][5]){
				this.transformRot.rotateX(-PI/2);
				}
	}
	
creator.growthArrow.prototype.moveByPick = function(pickInfo,delta){
	this.transform.identity();
	var deltaXYZ = pickInfo[1].position;
	if(pickInfo[3][0]){		deltaXYZ[0]+=delta;}
	else if(pickInfo[3][1]){deltaXYZ[0]-=delta;}
	else if(pickInfo[3][2]){deltaXYZ[1]+=delta;}
	else if(pickInfo[3][3]){deltaXYZ[1]-=delta;}
	else if(pickInfo[3][4]){deltaXYZ[2]+=delta;}
	else{					deltaXYZ[2]-=delta;}
	this.transform.translate(deltaXYZ[0],deltaXYZ[1],deltaXYZ[2]);
	}
	
creator.growthArrow.prototype.clear = function(){
	this.visible = false;
	this.transform.parent = null;
	}
	
creator.growthArrow.prototype.updataLight = function(){
	 this.lightPositionParam.value = eyePosi;
	}
